﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;

namespace ParseXAML
{
    internal class Assemblies
    {
        Dictionary<string, Assembly> ListAssembly = new Dictionary<string, Assembly>();
        string SL3Dir = @"C:\Program Files\Reference Assemblies\Microsoft\Framework\Silverlight\v3.0";
        //        Dictionary<string, string> ListNamespaceByAssembly = new Dictionary<string, string>();
        string binDir = @"G:\test\bin\Debug";

        internal Assembly GetAssembly(string assemblyName)
        {
            Assembly v;
            if (!ListAssembly.TryGetValue(assemblyName, out v))
            {
                string FileName = System.IO.Path.Combine(binDir, assemblyName);
                if (!System.IO.File.Exists(FileName))
                {
                    FileName = System.IO.Path.Combine(SL3Dir, assemblyName);
                    if (!System.IO.File.Exists(FileName))
                    {
                        FileName = System.IO.Path.Combine(binDir, assemblyName + ".dll");
                        if (!System.IO.File.Exists(FileName))
                        {
                            FileName = System.IO.Path.Combine(SL3Dir, assemblyName + ".dll");
                            if (!System.IO.File.Exists(FileName))
                                return null;
                        }
                    }
                }
                v = Assembly.LoadFile(FileName);
                ListAssembly.Add(assemblyName, v);
            }
            return v;
        }

        internal Type GetType(string assemblyName, string localNamespace, string typeName)
        {
            Assembly v = GetAssembly(assemblyName);
            if (v == null)
                return null;
            return v.GetType(localNamespace + "." + typeName);
        }

        static Assemblies _Assemblies = null;

        internal static Assemblies Current
        {
            get
            {
                if (_Assemblies == null)
                    _Assemblies = new Assemblies();
                return _Assemblies;
            }
        }

    }
}
